﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataTransfer;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Criterion;

namespace DataAccessLayer
{
    public class NHibernateDataProvider
    {
        private static ISession GetSession()
        {
            ISessionFactory sessionFactory = (new Configuration()).Configure().BuildSessionFactory();
            ISession session = sessionFactory.OpenSession();
            return session;
        }
        public Customer GetCustomerById(int CustomerId)
        {

            ISession session = GetSession();
            return session.Get<Customer>(CustomerId);
        }
        public void AddCustomer(Customer customer)
        {
            ISession session = GetSession();
            Customer NewCustomer = new Customer();
            NewCustomer.FirstName = customer.FirstName;
            NewCustomer.LastName = customer.LastName;
           
            session.Save(NewCustomer);
            session.Flush();
            session.Close();

        }
        public IList<Customer> GetAllCustomerById(int CustomerId)
        {
            ISession session = GetSession();
            return session.CreateQuery(" from Customer c where c.CustomerId > :cid")
                .SetInt32("cid", CustomerId)
                .List<Customer>();
        }
        public IList<Customer> GetAllCustomers()
        {
            ISession session = GetSession();
            return session.CreateQuery(" from Customer c")
               .List<Customer>();
            
        }
    }
}
